//
//  PenInfo_TwoTabCell.swift
//  SmartPenSwift
//
//  Created by tql on 2021/3/3.
//

import UIKit

class PenInfo_TwoTabCell: UITableViewCell {
    
    lazy var bgView : UIView = {
        let view : UIView = UIView.init()
        view.backgroundColor = .white
        view.layer.cornerRadius = 10
        return view
    }()
    lazy var customeIDLabel : UILabel = {
        let label = UILabel.init()
        label.textColor = UIColor(hex: "9AA0A6")
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    lazy var remainPowerLabel : UILabel = {
        let label = UILabel.init()
        label.textColor = UIColor(hex: "9AA0A6")
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    lazy var useMemoryLabel : UILabel = {
        let label = UILabel.init()
        label.textColor = UIColor(hex: "9AA0A6")
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    lazy var RCTTimeLabel : UILabel = {
        let label = UILabel.init()
        label.textColor = UIColor(hex: "9AA0A6")
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    
    typealias CallBackBlock = (_ tagIndex : Int) ->()
    var btnclickBlock : CallBackBlock?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle  = .none
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createUI() -> Void {
        self.backgroundColor = .clear
        self.contentView.addSubview(self.bgView)
        self.bgView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self.contentView).inset(10)
            make.bottom.equalTo(self.contentView).offset(-10)
            make.height.equalTo((44*4))
        }
        let titles : [String] = ["客户ID","剩余电量","已用内存","RTC时间"]
        var topGap : CGFloat = 0
        for i : Int  in 0..<titles.count {
            let label : UILabel = {
                let lb : UILabel = UILabel.init()
                lb.text = titles[i]
                lb.font = UIFont.systemFont(ofSize: 16)
                lb.textColor = UIColor(hex: "151515")
                return lb
            }()
            self.bgView.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.right.equalTo(self.bgView).inset(10)
                make.top.equalTo(self.bgView).offset(topGap)
                make.height.equalTo(44)
            }
            topGap += 44
            if i != titles.count - 1 {
                let line : UIView = {
                    let view : UIView = UIView.init()
                    view.backgroundColor = UIColor(hex: "E7EBF4")
                    return view
                }()
                self.bgView.addSubview(line)
                line.snp.makeConstraints { (make) in
                    make.left.right.equalTo(self.bgView).inset(10)
                    make.height.equalTo(1)
                    make.bottom.equalTo(label).offset(-1)
                }
                
            }
            if i == 0 {
                self.bgView.addSubview(self.customeIDLabel)
                self.customeIDLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(label)
                    make.right.equalTo(self.bgView).offset(-10)
                }
                
           
            }else  if i == 1 {
                self.bgView.addSubview(self.remainPowerLabel)
                self.remainPowerLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(label)
                    make.right.equalTo(self.bgView).offset(-10)
                }
            }else  if i == 2 {
                self.bgView.addSubview(self.useMemoryLabel)
                self.useMemoryLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(label)
                    make.right.equalTo(self.bgView).offset(-10)
                }
            }else  if i == 3 {
                self.bgView.addSubview(self.RCTTimeLabel)
                self.RCTTimeLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(label)
                    make.right.equalTo(self.bgView).offset(-30)
                }
                let arrowImageView : UIImageView = UIImageView.init()
                self.bgView.addSubview(arrowImageView)
                arrowImageView.image = UIImage.init(named: "icon-展开更多")
                arrowImageView.snp.makeConstraints { (make) in
                    make.centerY.equalTo(label)
                    make.left.equalTo(self.customeIDLabel.snp_rightMargin)
                    make.width.height.equalTo(20)
                }
                
                let btn: UIButton = UIButton.init()
                self.bgView.addSubview(btn)
                btn.snp.makeConstraints { (make) in
                    make.edges.equalTo(label)
                }
                btn.tag  = i + 100
                btn.addTarget(self, action: #selector(btnclick(_:)), for: .touchUpInside)
            }
            
            
        }
    }
    
    @objc func btnclick(_ btn : UIButton){
        debugPrint(#function)
        if btnclickBlock != nil  {
            btnclickBlock!(btn.tag - 100)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
